1203E - Boxers - CodeForces Solution


greedy sortings *1500

Please click on ads to support us..

Python Code:

n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
count = 0
last = 10**10

for i in range(n):
    cur = arr[i]
    if cur + 1 < last:
        last = cur + 1
        count += 1
    elif cur < last:
        last = cur
        count += 1
    elif cur - 1 != 0 and cur - 1  < last:
        last = cur - 1
        count += 1

print(count)

C++ Code:

// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    sort(v.begin(),v.end());
    
    unordered_set<int> s;
    for(int i=0;i<n;i++){
        int a=v[i];
        if(a==1){
            if(s.count(a)==0)
                s.insert(a);
            else s.insert(a+1);
        }else{
            if(s.count(a-1)==0){
                s.insert(a-1);
            }else if(s.count(a)==0){
                s.insert(a);
            }else{
                s.insert(a+1);
            }
        }
    }
    // for(auto &a:s){
    //     cout<<a<<" ";
    // }
    // cout<<endl;    
    cout<<(int)s.size()<<endl;
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1263A - Sweet Problem
1332B - Composite Coloring
254A - Cards with Numbers
215A - Bicycle Chain
1288B - Yet Another Meme Problem
1201C - Maximum Median
435A - Queue on Bus Stop
1409B - Minimum Product
723B - Text Document Analysis
1471C - Strange Birthday Party
1199A - City Day
1334A - Level Statistics
67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays